/**
 * @huanhuan
 * 受让合同
 */

import React, {Component} from 'react'
import {
    Text,
    View,
    Image,
    TextInput,
    ScrollView,
    StyleSheet,
    TouchableOpacity,
    FlatList,
    StatusBar,
    WebView,
    Platform,
    DeviceEventEmitter,
    BackHandler
} from 'react-native'
import NetRequest from '../../utils/utilsRequest'
import ServicesApi from '../../constants/ServicesApi'
import GlobalStyles from '../../constants/GlobalStyles'
import NavigationBar from '../../components/common/NavigationBar'
import UtilsView from '../../utils/utilsView'
import { toastShort, consoleLog } from '../../utils/utilsToast'
import ActivityIndicatorItem from '../../components/common/ActivityIndicatorItem'
import AlertManager from '../../components/common/AlertManager'
import {Overlay, Label, Button} from 'teaset';

import FooterComponent from '../../components/common/footerComponent'
import EmptyComponent from '../../components/common/emptyComponent'
export default class Contract extends Component {

    constructor(props) {
        super(props);
        const { params } = this.props.navigation.state;
        this.state =  {
            id: params.id, 
            money: params.money,
            price: params.price,
            datum_rate: params.datum_rate,
            dataSource: [],
        }
        // this.netRequest = new NetRequest();
    }

    componentDidMount() {
        this.getDataSource()
        
    }

    getDataSource = () => { // 请求转让详情
        let {id} = this.state;
        let url = ServicesApi.fund_order_assignee_contract;
        let data = { 
            id: id 
        };
        Services.post(url, data, true)
            .then( result => {
                if (result && result.code == StatusCode.SUCCESS_CODE) {
                    this.setState({
                        dataSource: result.data,
                    })
                }else{
                    toastShort(result.msg);
                }
            })
            .catch( error => {
                toastShort('服务器请求失败，请稍后重试！');
            })
    }

    onBack = () => {
        this.props.navigation.state.params.onCallBack();
        this.props.navigation.goBack();
    }

    onPushNavigator = (compent) => {
        const { navigate } = this.props.navigation;
        navigate( compent , {
            id: this.state.id,
            onCallBack:()=>{
                console.log('1');
            }
        })
    }

    render(){
        let {dataSource} = this.state;
        return (
            <View style={styles.container}>

                <NavigationBar
                    title = {'合同演示'}
                    style = {styles.navigationBar}
                    titleStyle = {{color: '#333333'}}
                    leftButton = {UtilsView.getLeftBlackButton(() => this.onBack())}
                />
                    <Overlay.PullView side='bottom' modal={false} ref={v => this.overlayView = v}>
                        <View style={{backgroundColor: '#fff', width: GlobalStyles.width, height: GlobalStyles.height}}>
                            <WebView
                                startInLoadingState={true}
                                source={{uri: dataSource.show_url}}
                                style={styles.webContainer}
                            />
                            <View style={styles.footer}>
                                <TouchableOpacity 
                                    style={styles.footerBtn1} 
                                    onPress={() => this.onBack()}
                                >
                                    <Text style={styles.footerBtnText1}>取消</Text>
                                </TouchableOpacity>
                                <TouchableOpacity 
                                    style={styles.footerBtn2}
                                    onPress={() => this.onPressYes()}
                                >
                                    <Text style={styles.footerBtnText2}>同意进行下一步</Text>
                                </TouchableOpacity>
                            </View> 
                        </View>
                    </Overlay.PullView>

                {/* <Text style={styles.ss}>{JSON.stringify(dataSource)}</Text> */}

            </View>
        );
    }

    onPressYes = () => {
        let {dataSource} = this.state;
        let url = ServicesApi.contract_create_pdf;
        let data = {
            url: dataSource.ourl,
            style: dataSource.info.style,
            fund_id: dataSource.info.fund_id,
        };
        AlertManager.showPopView(<View style={[GlobalStyles.loadingview, GlobalStyles.flexColumnCenter]}><Image source={require('../../assets/images/icons/loading.gif')} style={GlobalStyles.loading} /><Text style={GlobalStyles.loadingtext}>文件生成中，请稍候...</Text></View>, { modal:true })
        return Services.post(url, data)
            .then( result => {
                AlertManager.hide();
                this.onPushNavigator('TransferDetatil')
            })
            .catch( error => {
                AlertManager.hide();
            })
    }
}

const styles = StyleSheet.create({
    // 整体
    container: {
        flex: 1,
        backgroundColor: '#fff',
    },
    navigationBar: {
        backgroundColor: '#fff',
        borderBottomColor: GlobalStyles.bgColor,
        borderBottomWidth: 1,
    },
    webContainer: {
        width: GlobalStyles.width,
        // height: GlobalStyles.height,
        flex: 1,
        // backgroundColor: '#fff',
        // marginTop: Platform.OS === 'ios' ? -15 : 0,
        marginTop: 80,
    },

    titleBox: {
        alignItems: 'center',
        justifyContent: 'center',
        height: 50,
    },
    title: {
        color: '#333',
        fontSize: 16,
        fontWeight: '600',
    },
    mainTextBox: {
        // paddingHorizontal: 15,
        // marginBottom: 10,
    },
    mainText: {
        color: '#666',
        fontSize: 12,
        marginHorizontal: 15,
        marginBottom: 10,
    },
    mainImg: {
        width: 50,
        height: 50,
        marginBottom: 10,
        marginLeft: 15,
    },
    footer: {
        // position: 'absolute',
        // left: 0,
        // bottom: 0,
        width: GlobalStyles.width,
        height: 50,
        flexDirection: 'row',
        borderTopWidth: 1,
        borderTopColor: '#f2f2f2',
    },
    footerBtn1: {
        flex: 1,
        height: 50,
        backgroundColor: '#fff',
        justifyContent: 'center',
        alignItems: 'center',
    },
    footerBtnText1: {
        color: '#333',
        fontSize: 14,
    },
    footerBtn2: {
        flex: 3,
        height: 50,
        backgroundColor: '#b88950',
        justifyContent: 'center',
        alignItems: 'center',
    },
    footerBtnText2: {
        color: '#fff',
        fontSize: 14,
    },
});




            







